/*     */ package com.newcrm.db;
/*     */ 
/*     */ import com.newcrm.exception.DBException;
/*     */ import java.sql.Connection;
/*     */ import java.sql.SQLException;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import org.apache.commons.dbutils.DbUtils;
/*     */ import org.apache.commons.dbutils.GenKeyQueryRunner;
/*     */ import org.apache.commons.dbutils.MyMapListHandler;
/*     */ import org.apache.commons.dbutils.QueryRunner;
/*     */ import org.apache.commons.dbutils.handlers.ScalarHandler;
/*     */ import org.apache.log4j.Logger;
/*     */ 
/*     */ public class DealCategoryManager
/*     */ {
/*  19 */   private static Logger logger = Logger.getLogger(DealCategoryManager.class
/*  20 */     .getName());
/*     */ 
/*     */   public List<Map<String, Object>> list(int companyId)
/*     */     throws DBException, SQLException
/*     */   {
/*  32 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  33 */     if (conn == null) {
/*  34 */       throw new DBException();
/*     */     }
/*     */ 
/*  37 */     QueryRunner runner = new QueryRunner();
/*     */     try {
/*  39 */       return (List)runner.query(conn, "SELECT DealCategoryId,DealCategoryName,Color FROM T_Deal_Category_Info WHERE CompanyId = ? ORDER BY OrderNum ASC", 
/*  40 */         new MyMapListHandler(), new Object[] { Integer.valueOf(companyId) });
/*     */     } finally {
/*     */       try {
/*  43 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/*  45 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   public int add(int companyId, String name, String userId)
/*     */     throws DBException, SQLException
/*     */   {
/*  62 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  63 */     if (conn == null) {
/*  64 */       throw new DBException();
/*     */     }
/*  66 */     GenKeyQueryRunner runner = new GenKeyQueryRunner(
/*  67 */       new ScalarHandler(), new String[] { "DealCategoryId" });
/*     */     try {
/*  69 */       runner.update(conn, "INSERT INTO T_Deal_Category_Info (DealCategoryName, CompanyId, CreateDate, CreateUserId) VALUES (?, ?, NOW(), ?)", new Object[] { name, Integer.valueOf(companyId), 
/*  70 */         userId });
/*  71 */       return ((Long)runner.getGeneratedKeys()).intValue();
/*     */     } finally {
/*     */       try {
/*  74 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/*  76 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   public int update(int id, String name, String color, String userId)
/*     */     throws DBException, SQLException
/*     */   {
/*  94 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  95 */     if (conn == null) {
/*  96 */       throw new DBException();
/*     */     }
/*  98 */     QueryRunner runner = new QueryRunner();
/*     */     try
/*     */     {
/*     */       int i;
/* 101 */       if ((name == null) || (name.trim().length() == 0)) {
/* 102 */         return runner.update(conn, "UPDATE T_Deal_Category_Info SET Color = ?, UpdateDate = NOW(), UpdateUserId = ? WHERE DealCategoryId = ?", new Object[] { 
/* 103 */           color, userId, Integer.valueOf(id) });
/*     */       }
/* 105 */       return runner.update(conn, "UPDATE T_Deal_Category_Info SET DealCategoryName = ?, Color = ?, UpdateDate = NOW(), UpdateUserId = ? WHERE DealCategoryId = ?", new Object[] { name, 
/* 106 */         color, userId, Integer.valueOf(id) });
/*     */     } finally {
/*     */       try {
/* 109 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/* 111 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   public int delete(int categoryId)
/*     */     throws DBException, SQLException
/*     */   {
/* 125 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 126 */     if (conn == null) {
/* 127 */       throw new DBException();
/*     */     }
/* 129 */     QueryRunner runner = new QueryRunner();
/*     */     try
/*     */     {
/* 132 */       int result = runner.update(conn, "DELETE FROM T_Deal_Category_Info WHERE DealCategoryId = ?", 
/* 133 */         Integer.valueOf(categoryId));
/* 134 */       return result;
/*     */     } finally {
/*     */       try {
/* 137 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/* 139 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   public int updateSort(int categoryId, int order, String userId)
/*     */   {
/* 153 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 154 */     if (conn == null) {
/* 155 */       return 0;
/*     */     }
/* 157 */     QueryRunner runner = new QueryRunner();
/*     */     try
/*     */     {
/* 160 */       return runner.update(conn, "UPDATE T_Deal_Category_Info SET OrderNum = ? , UpdateDate = NOW(), UpdateUserId = ? WHERE DealCategoryId = ?", new Object[] { 
/* 161 */         Integer.valueOf(order), userId, Integer.valueOf(categoryId) });
/*     */     } catch (SQLException e) {
/* 163 */       logger.warn("update order failed", e);
/*     */     } finally {
/*     */       try {
/* 166 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/* 168 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/* 171 */     return 0;
/*     */   }
/*     */ }

/* Location:           C:\E\referenceProject\CRM参考\qikebao\WEB-INF\lib\qikebao.jar
 * Qualified Name:     com.newcrm.db.DealCategoryManager
 * JD-Core Version:    0.6.1
 */